function selectAdd(){
	var value = $("[name='addtable']:visible").val();
	if(value == "m"){
		$(".multiple_addtable").attr("style","display:");
		$(".single_addtable").attr("style","display:none");
	}else{
		$(".single_addtable").attr("style","display:");
		$(".multiple_addtable").attr("style","display:none");
	}
}


function gotoaddTable(dom,posId,zoneId){
	var dataSet = {posId:posId,zoneId:zoneId};
	var dialog = editAdminDataDialog("left,top",dom,label_addZone,"/adminconsole/POSManager/POSTable/AddTable.jsp","addDialogZone",AddEventClickTable,loadElementFinishTable,dataSet);
}
function gotoeditTable(dom,posId,zoneId,id){
	var dataSet = {posId:posId,zoneId:zoneId,id:id};
	var dialog = editAdminDataDialog("left,top",dom,label_addZone,"/adminconsole/POSManager/POSTable/EditTable.jsp","addDialogZone",AddEventClickTable,loadElementFinishTableEdit,dataSet);
}
function loadElementFinishTable(dialog,dataSet){
	dialog.parents(".ui-dialog:first").css("width",850);
	setOptionTable(dataSet);
}
function loadElementFinishTableEdit(dialog,dataSet){
	dialog.parents(".ui-dialog:first").css("width",500);
	setOptionTable(dataSet);
	EditTable(dialog,dataSet.id);
}

function EditTable(dialog,id){
	if(id != null){
		var url = "/POSTableService";
		var data = { func : "getTableById",
				id : id
			}
		simpleserviceWithArgs(url,data,EditTableSuccess,dialog);
	}
}

function EditTableSuccess(data,dialog){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	dialog.find("[name=single_nametable]").val(objArray['tableTitle']);
}

function setOptionTable(dataSet){
	var url = "/POSTableService";
	var data = {func : "getAllZone",
				posId:dataSet.posId
				}
	simpleserviceWithArgs(url,data,setTableSuccess,dataSet);
}

function setTableSuccess(data,dataSet){
	var objArray = new Array();
	objArray = JSON.parse(data);
	$("[name=zonename] option").remove();
	for (key in objArray) {
		$("[name=zonename]").append('<option value="' + objArray[key].id + '">' + objArray[key].title + '</option>');
	}
	$("[name=zonename]:visible option[value=" +dataSet.zoneId+"]").attr("selected","selected");
}

function AddEventClickTable(dialog,dataSet){
	var zonename = dialog.find("[name=zonename]").val();
	var typeAdd = dialog.find("[name=addtable]").val();
	var url = "/POSTableService";
	if(dataSet.id == null){
		var func = "addTable";
		dataSet	 = {posId:dataSet.posId,zoneId:zonename}
	}else{
		var func = "editTable";
		typeAdd="s";
	}
	if(typeAdd=="s"){
		var nametable = dialog.find("[name=single_nametable]").val();
		if(nametable!=""&&nametable!=null){
			var data = { func : func,
					id : dataSet.id,
					zonename:zonename,
					nametable : nametable,
					typeAdd:typeAdd
				}
			simpleserviceWithArgs(url,data,showPOSTable,dataSet);
		}
	}else{
		var typeAddRadio = dialog.find("[name=selecttype]:checked").val();
		if(typeAddRadio!=null&&typeAddRadio!=undefined){
			if(typeAddRadio=="typeComma"){
				var nametable = dialog.find("[name=multiple_nametable]").val();
				var data = { func : func,
						id : dataSet.id,
						zonename:zonename,
						nametable : nametable,
						typeAdd:typeAddRadio
					}
				simpleserviceWithArgs(url,data,showPOSTable,dataSet);
			}else{
				var prefix = dialog.find("[name=prefix]").val();
				var sumTable = dialog.find("[name=sumTable]").val();
				var to = dialog.find("[name=to]").val();
				var data = { func : func,
						id : dataSet.id,
						zonename:zonename,
						nametable : nametable,
						prefix : prefix,
						sumTable: sumTable,
						to:to,
						typeAdd:typeAddRadio
					}
				simpleserviceWithArgs(url,data,showPOSTable,dataSet);
			}
		}
	}
	
}

function changeStatusTable(id){
	waitingDialog({});
	var url = "/POSTableService";
	var data = { func : "editStatusPOSTable",
			tableId : id
		}
	simpleservice(url,data,changeStatusZoneSuccess);
}
function changeStatusTableSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	$("#status"+objArray["id"]).text(objArray["status"]);
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function DeleteTable(dataSet){
	waitingDialog({});
	var listCheckBox = "";
	for(var i = 0;i<$('tr [type=checkbox]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i].checked == true){	
			listCheckBox += $($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).attr("value")+",";
		}
	}
	var url = "/POSTableService";
	var data = { func : "delTable",
				tableId : listCheckBox.substring(0,listCheckBox.length - 1)
				}
	simpleserviceWithArgs(url,data,showPOSTable,dataSet);
}

function setLabelTable(posId,thisclick,zoneId){
	if(thisclick==undefined||thisclick==""){
		var addTitle = $("#titleName").html();
		var back = $("#posTitle").html();
	}else{
		var addTitle = $(thisclick).parent().parent().find("[name=titleName]").html();
		var back = $("#titleName").html();
	}
	var listCategory = "";
	var addCategory = "";
	var sendData = "";
	var delCategory = "";
	var titlePos = "<span class='link' id='posTitle' onclick='showPOSZone(\"\","+posId+",\"\",\"\",\"back\")'; >"+back+"</span>"+" /";
	$("#titleLinkPOSCategory").replaceWith("<div class='breadcrumb' id='titleLinkPOSCategory' >" +
			"<a href='/adminconsole/POSManager/PointPos/PointPos.jsp'><span class='link'>"+titlePoint+"</span></a>/"+
			titlePos+
			'<span id="titleName" >'+addTitle+"</span></div>");
	$("#suggestCategory").replaceWith("<div class='destitle' id='suggestCategory'>"+suggestTable+"</div>");
	$("#listCategory").replaceWith("<div class='titlelist' id='listCategory'>"+listTable+"<span id='listsize'></span></div>");
	$("#addCategory").replaceWith("<span id='addCategory'><input class='addbtn' name='addtaxes' value=\""+addTable+"\" type='button' onClick=\"gotoaddTable($(this),"+posId+","+zoneId+");\"></span>");
	$("#delCategory").replaceWith("<span style='padding-left:10px' id='delCategory'><input class='deletebtn' name='deletetaxes' value=\""+deleteTable+"\" type='button' onClick='DeleteTable({posId:"+posId+",zoneId:"+zoneId+"})'></span>");
}

function showPOSTable(data,dataSet,thisclick,id){
	debugger;
	var objArray = new Array();
	if(data!=null&&data!="")
		objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(dataSet.posId==undefined){
		var posId = dataSet;
		var zoneId = data;
		dataSet = {posId:posId,zoneId:zoneId};
	}else{
		var posId = dataSet.posId;
		var zoneId = dataSet.zoneId;
	}
	setLabelTable(posId,thisclick,zoneId);
	if(id == null||id==""){
		if($("[name=maxEntries]").val() == null){
			var line = 10;
		}else{
			var line = $("[name=maxEntries]").val();
		}
		if($("[name=selectpage]").val() == null){
			var pagePointer = 1; 
		}else{
			var pagePointer = $("[name=selectpage]").val(); 
		}
	}else if(id == "next"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())+1;
	}else if(id == "previous"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())-1;		
	}
	var link = "/POSTableService";
	var data = { func: "getAllTableInZone",
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer,
				zoneId: zoneId
			   }
	simpleserviceWithArgs(link,data,showPOSTableList,dataSet);
}

function showPOSTableList(data,dataSet){
	$("[id=showList]").load("/adminconsole/POSManager/POSTable/POSTable.jsp",function(responseText, textStatus, XMLHttpRequest) {
		var objArray2 = new Array();
		objArray2 = JSON.parse(data);
		var sumtext = "";
		var text = "";
		var pageAmount = Math.ceil(objArray2[0]/objArray2[2]);
		var line = objArray2[2];
		var pagePointer = objArray2[1];
		var objArray = objArray2[3];
		$("#listsize").html("("+objArray.length+")");
		for(var i = 0;i< objArray.length+1;i++){
				if(i != objArray.length ){
					text = "<tr id=\"row_"+objArray[i].id+"\" style=\"border: 0px;\"> " +
							"<td id=\"col_1\" align=\"center\"> "+(i+1)+". </td>"+
							"<td><input type=\"checkbox\" id=\"id_"+i+"\" name=\"ids[]\" value=\""+objArray[i].id+"\"> </td>"+
							"<td align=\"left\" name=\"titleName\"> "+objArray[i].tableTitle+" </td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"gotoeditTable($(this),"+dataSet.posId+","+dataSet.zoneId+","+objArray[i].id+")\">Edit</a></td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"changeStatusTable("+objArray[i].id+") \"> " +
							"<span id =status"+objArray[i].id+">"+objArray[i].status+"</a>" +
							"</td>" +
							"</tr>";
					sumtext += text;
				}else{
					text = "<tr>"+
					"<td colspan=\"13\" style=\"padding:0;\">"+
					"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
					"<tbody>"+
						"<tr>"+
							"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
								"<select name=\"selectpage\" onchange=\"showPOSTable("+dataSet.zoneId+","+dataSet.posId+")\">"+
								"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
							"</td>"+
							"<td width=\"31%\" align=\"center\">"+foor_view+
								"<select name=\"maxEntries\" onchange=\"showPOSTable("+dataSet.zoneId+","+dataSet.posId+")\">"+
									"<option style=\"color:#000000\"  value=\"10\">10</option>"+
									"<option style=\"color:#000000\"  value=\"20\">20</option>"+
									"<option style=\"color:#000000\"  value=\"30\">30</option>"+
									"<option style=\"color:#000000\"  value=\"50\">50</option>"+
									"<option style=\"color:#000000\"  value=\"70\">70</option>"+
									"<option style=\"color:#000000\"  value=\"100\">100</option>"+
									"<option style=\"color:#000000\"  value=\"200\">200</option>"+
									"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
								"</select>"+foor_rpp+" </td>" +
								"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
								" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
								"</tbody></table></td></tr>";
					sumtext += text;
				}
		}
		$("#showList tr").not($("#showList tr").first()).remove();
		$("#showList").append(sumtext);
		$("#pageAmount").text(pageAmount);
		if(pagePointer+1 <= pageAmount){
			$("#next").attr("onclick","showPOSTable("+dataSet.zoneId+","+dataSet.posId+",'',id)");
			$("#next").html("<a href=\"#\">Next&gt;&gt;</a>");
		}
		if(pagePointer > 1){
			$("#previous").attr("onclick","showPOSTable("+dataSet.zoneId+","+dataSet.posId+",'',id)");
			$("#previous").html("<a href=\"#\">&lt;&lt;Previous</a>");
		}
		$("[name=maxEntries]").val(line);
		for ( var i = 0; i < pageAmount; i++) {
			$("[name=selectpage]").append(
					$("<option>").val((i+1)).html((i+1)));
			}
		$("[name=selectpage]").val(pagePointer);
	});
	closeWaitingDialog();
}